package skills.jssvc.deu.cn.skillsecond.programming2;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import skills.jssvc.deu.cn.skillsecond.MyApplication;
import skills.jssvc.deu.cn.skillsecond.R;
import skills.jssvc.deu.cn.skillsecond.adapter.Frag2RoadAdapter;
import skills.jssvc.deu.cn.skillsecond.bean.TrafficLightConfig;
import skills.jssvc.deu.cn.skillsecond.entry.Road;
import skills.jssvc.deu.cn.skillsecond.util.HttpRequest;

public class RedMangFragment extends Fragment {
    private final String TAG = "RedMangFragment";
    private View view;

    private Spinner sortSpinner;
    private Button queryBtn;
    private RecyclerView recyclerView;
    private LinearLayoutManager manager;
    private Frag2RoadAdapter adapter;
    private List<Road> roadList;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment2_red_mang, container, false);
        initView();
        setListeners();
        return view;
    }

    private void initView(){
        sortSpinner = (Spinner) view.findViewById(R.id.frag2_redmang_sortSpinner);
        queryBtn = (Button) view.findViewById(R.id.frag2_redmang_query_btn);
        recyclerView = (RecyclerView) view.findViewById(R.id.frag2_road_recyclerview);
        manager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(manager);
        roadList = new ArrayList<>();
        query(1);
        adapter = new Frag2RoadAdapter(roadList);
        recyclerView.setAdapter(adapter);
    }

    private void setListeners(){
        queryBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sort(sortSpinner.getSelectedItemPosition());
            }
        });

    }

    private void query(final int road){
        String param = "{\"TrafficLightId\":\""+String.valueOf(road)+"\", \"UserName\":\"user1\"}";
        HttpRequest.post("GetTrafficLightConfigAction", param, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                TrafficLightConfig config = new Gson().fromJson(jsonObject.toString(),TrafficLightConfig.class);
                Log.d(TAG, "onResponse: road="+road);
                if(!config.getRESULT().equals("S")){
                    adapter.notifyDataSetChanged();
                    return;
                }else {
                    roadList.add(new Road(road,config.getRedTime(),config.getYellowTime(),config.getGreenTime()));
                    query(road+1);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(MyApplication.appContext,"查询错误!",Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void sort(int type){
        switch (type){
            case 0:
                Collections.sort(roadList, new Comparator<Road>() {
                    @Override
                    public int compare(Road road, Road t1) {
                        return new Integer(road.getRoad()).compareTo(t1.getRoad());
                    }
                });
                break;
            case 1:
                Collections.sort(roadList, new Comparator<Road>() {
                    @Override
                    public int compare(Road road, Road t1) {
                        return new Integer(t1.getRoad()).compareTo(road.getRoad());
                    }
                });
                break;
            case 2:
                Collections.sort(roadList, new Comparator<Road>() {
                    @Override
                    public int compare(Road road, Road t1) {
                        return new Integer(road.getRedTime()).compareTo(t1.getRedTime());
                    }
                });
                break;
            case 3:
                Collections.sort(roadList, new Comparator<Road>() {
                    @Override
                    public int compare(Road road, Road t1) {
                        return new Integer(t1.getRedTime()).compareTo(road.getRedTime());
                    }
                });
                break;
            case 4:
                Collections.sort(roadList, new Comparator<Road>() {
                    @Override
                    public int compare(Road road, Road t1) {
                        return new Integer(road.getYellowTime()).compareTo(t1.getYellowTime());
                    }
                });
                break;
            case 5:
                Collections.sort(roadList, new Comparator<Road>() {
                    @Override
                    public int compare(Road road, Road t1) {
                        return new Integer(t1.getYellowTime()).compareTo(road.getYellowTime());
                    }
                });
                break;
            case 6:
                Collections.sort(roadList, new Comparator<Road>() {
                    @Override
                    public int compare(Road road, Road t1) {
                        return new Integer(road.getRedTime()).compareTo(t1.getRedTime());
                    }
                });
                break;
            case 7:
                Collections.sort(roadList, new Comparator<Road>() {
                    @Override
                    public int compare(Road road, Road t1) {
                        return new Integer(t1.getRedTime()).compareTo(road.getRedTime());
                    }
                });
                break;
            default:break;
        }
        adapter.notifyDataSetChanged();
    }

}
